import Cookies from 'js-cookie'

const state = {
    sidebar: {
        // opened: Cookies.get('sidebarStatus') ? !!+Cookies.get('sidebarStatus') : true,
        opened: true,
        withoutAnimation: false
    },
    device: 'desktop',
    routers: [],
    pageOpenedList: [],
    currentPageName: 'home',
    noCachePages: [],
    nowTheme: {
        nowThemeLeft: 'white',
        nowThemeRight: 'white'
    },
    nowMenu: 'home',
    breadcrumb: null,
    currMenu: 'home',
    nowSelectOldMenu: ''
}
const mutations = {
    TOGGLE_SIDEBAR: (state) => {
        state.sidebar.opened = !state.sidebar.opened
        state.sidebar.withoutAnimation = false
        if (state.sidebar.opened) {
            Cookies.set('sidebarStatus', 1)
        } else {
            Cookies.set('sidebarStatus', 0)
        }
    },
    CLOSE_SIDEBAR: (state, withoutAnimation) => {
        Cookies.set('sidebarStatus', 0)
        state.sidebar.opened = false
        state.sidebar.withoutAnimation = withoutAnimation
    },
    TOGGLE_DEVICE: (state, device) => {
        state.device = device
    },
    addPageList: (state, obj) => {
        state.pageOpenedList.push(obj)
        sessionStorage.setItem('pageOpenedList', JSON.stringify(state.pageOpenedList))
    },
    initPageList: (state, listString) => {
        if (listString && JSON.parse(listString).length) {
            state.pageOpenedList = JSON.parse(listString)
        } else {
            state.pageOpenedList = [
                {
                    name: '首页',
                    path: 'home',
                    title: '首页'
                }
            ]
        }
    },
    updatePageList(state, get) {
        // let openedPage = state.pageOpenedList[get.index];
        state.pageOpenedList.splice(get.index, 1)
        // 保持最新打开的页面在tag最后一项
        state.pageOpenedList.push(get)
    },
    setCurrentPageName: (state, name) => {
        state.currentPageName = name || 'home'
    },
    removeTag: (state, obj) => {
        let index = state.pageOpenedList.findIndex((item) => item.name === obj.name)
        state.pageOpenedList.splice(index, 1)
        // 关闭页面是否重新加载父级
        if (obj.fresh && obj.nextName) {
            state.noCachePages.push(obj.nextName)
        }
    },
    clearNoCachePages(state, obj) {
        let index = state.noCachePages.findIndex((item) => item === obj)
        state.noCachePages.splice(index, 1)
    },
    clearAllTags(state) {
        state.pageOpenedList.splice(1)
    },
    clearOtherTags(state, vm) {
        let currentName = vm.$route.name
        let currentIndex = 0
        state.pageOpenedList.forEach((item, index) => {
            if (item.name === currentName) {
                currentIndex = index
            }
        })
        if (currentIndex === 0) {
            state.pageOpenedList.splice(1)
        } else {
            state.pageOpenedList.splice(currentIndex + 1)
            state.pageOpenedList.splice(1, currentIndex - 1)
        }
    },
    updateTheme(state, nowTheme) {
        state.nowTheme = nowTheme
    },
    updateNowMenu(state, nowMenu) {
        sessionStorage.setItem('nowMenu', JSON.stringify(nowMenu))
        state.nowMenu = nowMenu
    },
    updateNowBreadcrumb(state, breadcrumb) {
        state.breadcrumb = breadcrumb
    },
    updateCurrMenu(state, currMenu) {
        state.currMenu = currMenu
    },
    updateNowSelectOldMenu(state, nowSelectOldMenu) {
        state.nowSelectOldMenu = nowSelectOldMenu
    }
}

const actions = {
    toggleSideBar({ commit }) {
        commit('TOGGLE_SIDEBAR')
    },
    closeSideBar({ commit }, { withoutAnimation }) {
        commit('CLOSE_SIDEBAR', withoutAnimation)
    },
    toggleDevice({ commit }, device) {
        commit('TOGGLE_DEVICE', device)
    },
    updateMenuList({ commit }, menus) {
        commit('UPDATE_MENU_LIST', menus)
    },
    updateAppRouter({ commit }, router) {
        console.log(router)
        commit('UPDATE_APP_ROUTER', router)
    }
}

export default {
    namespaced: true,
    state,
    mutations,
    actions
}
